const jsonServer = require('json-server')


const db = require('./db.js')
const routes = require('./routes.js')
const port = 8083;

const server = jsonServer.create()
const router = jsonServer.router(db)
const middlewares = jsonServer.defaults()
const rewriter = jsonServer.rewriter(routes)

server.use(jsonServer.bodyParser)
server.use(middlewares)
server.use(rewriter) // 注意：rewriter 的设置一定要在 router 设置之前


var ORGS = require('./org.js');
var Mock = require('mockjs');

server.get('/api/plat/org-description',ORGS.desc);



// 将 POST 请求转为 GET
server.use((request, res, next) => {
	console.log(request.body)
    
    if (request.method === 'POST') {	
        request.body['createdAt'] = Date.now()
    }
    request.method = 'GET';
    res.header('X-Hello', 'World')
    next();
    res.header('X-Hello', 'World2')
    
})


server.use(router)  
server.listen(port, () => {
    console.log('open mock server at localhost:' + port)
})